//
//  PywForgetCheckPhoneViewController.m
//  PywSdk
//
//  Created by smallwave on 16/8/4.
//  Copyright © 2016年 zero. All rights reserved.
//

#import "PywForgetCheckPhoneViewController.h"
#import "PywTextFieldView.h"
#import "UIButton+Pyw.h"
#import "UIImage+Pyw.h"
#import "UILabel+Pyw.h"
#import "PywMasonry.h"
#import "PywCommonManager.h"
#import "PywMBProgressHUD+Pyw.h"
#import "PywGetSmsCodeByUsernameApi.h"
#import "PywSdk+login.h"
#import "PywForgetValidCodeViewController.h"
#import "UILabel+PywAttributeTextTapAction.h"
#import "MutableDictionary.h"
#import "PywBuriedManager.h"

@interface PywForgetCheckPhoneViewController ()<PywAttributeTapActionDelegate>

@property (nonatomic, strong) PywTextFieldView  *gameOrPhoneTF; // 游戏账号|手机号
@property (nonatomic, strong) UIButton          *validCodeBtn;  // 获取验证码按钮
@property (nonatomic, strong) UILabel           *reminderLabel; // 温馨提示Label

@property (nonatomic, strong) PywGetSmsCodeByUsernameApi  *getSmsCodeByUsernameApi;
@property (nonatomic, copy) NSString *smsType;

@end

@implementation PywForgetCheckPhoneViewController
KeyBoardHelper(90)

#pragma mark - Lift Cycle Method
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUp];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    NSLog(@"%s",__FUNCTION__);
    [PywBuriedManager manager].page_sn = @"";
}
- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    NSLog(@"%s",__FUNCTION__);
    [PywBuriedManager manager].page_sn = @"1";
}

#pragma mark - UI Interface
- (void)setUp{
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"验证绑定手机";
    self.navigationItem.rightBarButtonItem = nil;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self addSubviewAndLayoutSubviews];
}
- (void)addSubviewAndLayoutSubviews{
    
    [self.view addSubview:self.gameOrPhoneTF];
    [self.view addSubview:self.validCodeBtn];
    [self.view addSubview:self.reminderLabel];
    
    __weak typeof(self) wSelf = self;
    [self.gameOrPhoneTF mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.view.mas_top).offset(80);
        make.width.equalTo(wSelf.view.mas_width).multipliedBy(0.85);
        make.height.equalTo(@40);
    }];
    
    [self.validCodeBtn mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.gameOrPhoneTF.mas_bottom).offset(40);
        make.width.height.equalTo(wSelf.gameOrPhoneTF);
    }];
    
    [self.reminderLabel mas_makeConstraints:^(PywMASConstraintMaker *make) {
        make.centerX.offset(0);
        make.top.equalTo(wSelf.validCodeBtn.mas_bottom).offset(20);
    }];
}

#pragma mark - Action
- (void)getValidCodeCilck{
    [self.view endEditing:YES];
    // 判断输入手机号 是否合法
    NSString *username = [self.gameOrPhoneTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (username.length <= 0) {
        
        [PywMBProgressHUD showError:@"请输入游戏账号/手机号"];
    } else {
        [self.getSmsCodeByUsernameApi requestGetSmsCodeByUsernameWithParams:[self requestParams] success:^(NSString *status, id anyObjct) {
            NSString *passport = [NSString stringWithFormat:@"%@",anyObjct[@"passport"]];
            NSInteger is_reg = [[anyObjct objectForKey:@"is_reg"] integerValue];
            if (is_reg == 1) {
                // 跳转控制器
                PywForgetValidCodeViewController *validCodeVC = [[PywForgetValidCodeViewController alloc] initWithPhoneNumber:passport smsType:self.smsType];
                [self.navigationController pushViewController:validCodeVC animated:YES];
            } else {
                [PywMBProgressHUD showError:@"手机号未注册" toView:[UIApplication sharedApplication].keyWindow];
            }
        } fail:^(NSError *error) {
            
        }];
    }
}
/**
 *  请求参数
 *
 *  @return <#return value description#>
 */
- (MutableDictionary *)requestParams{
    NSString *username = [self.gameOrPhoneTF.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    self.smsType = [NSString stringWithFormat:@"%d",SMSCODETYPE_NOTLOGIN_FORGETPWD];
    
    MutableDictionary *params = [[MutableDictionary alloc] init];
    [params setObject:username forKey:@"username"];
    [params setObject:self.smsType forKey:@"smstype"];
    return params;
}

#pragma mark - PywAttributeTapActionDelegate
- (void)pyw_attributeTapReturnString:(NSString *)string range:(NSRange)range index:(NSInteger)index{
    NSString *phone = @"4000709394";
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt://%@",phone]]];
}

#pragma mark - lazy
- (PywTextFieldView *)gameOrPhoneTF{
    if (!_gameOrPhoneTF) {
        _gameOrPhoneTF = [PywTextFieldView phoneOrAccountTextFieldWithLeftImg:@"icon_phone_normal"
                                                              TextPlaceholder:@"请输入游戏账号/手机号"];
        [_gameOrPhoneTF setAccessibilityId:@"fogotpwsd_game_account"];
    }
    return _gameOrPhoneTF;
}
- (UIButton *)validCodeBtn{
    if (!_validCodeBtn) {
        _validCodeBtn = [UIButton buttonWithBackgroundColor:RGB(252, 177, 14, 1)
                                               cornerRadius:20
                                                 titleColor:[UIColor whiteColor]
                                         titleLabelFontSize:18
                                                      title:@"获取验证码"];
        [_validCodeBtn addTarget:self action:@selector(getValidCodeCilck) forControlEvents:UIControlEventTouchUpInside];
        [_validCodeBtn setAccessibilityIdentifier:@"fogotpwsd_getcode"];
    }
    return _validCodeBtn;
}
- (UILabel *)reminderLabel{
    if (!_reminderLabel) {
        _reminderLabel = [UILabel labelWithText:nil
                                textAlignment:NSTextAlignmentCenter
                                     fontSize:12
                                    textColor:[UIColor blackColor]];
        
        _reminderLabel.attributedText = [self labelAttributedText];
        [_reminderLabel pyw_addAttributeTapActionWithStrings:@[@"联系客服"] delegate:self];
    }
    return _reminderLabel;
}
- (NSMutableAttributedString *)labelAttributedText{
    NSString *str = [NSString stringWithFormat:@"温馨提示：如游戏账号未绑定手机，请联系客服申诉"];
    NSMutableAttributedString *attributedStr = [[NSMutableAttributedString alloc] initWithString:str];
    
    CGFloat fontSize = [[PywCommonManager manager] changeDeviceTextFontSize:12];
    [attributedStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:fontSize] range:NSMakeRange(0, str.length)];
    [attributedStr addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(str.length - 6, 4)];
    // 添加下划线
    [attributedStr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(str.length - 6, 4)];
    // 设置行高
    NSMutableParagraphStyle *sty = [[NSMutableParagraphStyle alloc] init];
    sty.lineSpacing = 5;
    [attributedStr addAttribute:NSParagraphStyleAttributeName value:sty range:NSMakeRange(0, str.length)];
    return attributedStr;
}
- (PywGetSmsCodeByUsernameApi *)getSmsCodeByUsernameApi{
    if (!_getSmsCodeByUsernameApi) {
        _getSmsCodeByUsernameApi = [PywGetSmsCodeByUsernameApi manager];
    }
    return _getSmsCodeByUsernameApi;
}

@end
